package cn.clwxy.gdz.dao;

import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.clwxy.gdz.entity.Student;
import cn.clwxy.gdz.util.BaseDao;

public class StudentDaoImpl extends BaseDao implements StudentDao {

	@Override
	public List<Student> findStudent() {
		String sql = "SELECT * FROM student ";
		return super
				.queryList(sql, new BeanListHandler<Student>(Student.class));
	}

	@Override
	public List<Student> findStudentByGrade(String grade) {
		String sql = "SELECT * FROM student WHERE studentGrade=?";
		return super.queryList(sql,
				new BeanListHandler<Student>(Student.class), grade);
	}

	@Override
	public List<Student> findStudentByAddress(String address) {
		String sql = "SELECT b.studentName,b.studentPwd,b.studentGrade,b.studentSex,b.studentPhone"
				+ ",b.studentDesc,b.studentDate,b.`studentId` FROM  studentadderss "
				+ "a INNER JOIN student b ON  a.userId=b.studentId where addressDesc=?;";
		return super.queryList(sql,
				new BeanListHandler<Student>(Student.class), address);
	}

	@Override
	public List<Student> findStudentBySex(String sex) {
		String sql = "SELECT * FROM student WHERE studentsex=?";
		return super.queryList(sql,
				new BeanListHandler<Student>(Student.class), sex);
	}

	@Override
	public int addStudent(Student student) {
		String sql = "INSERT INTO student(studentPwd,studentName,studentGrade,studentSex,studentPhone,studentDesc,studentDate,studentImgId)"
				+ "VALUES('?','?','?','?','?','?','?','?')";
		return 0;
	}

	@Override
	public int registerStudent(Student student) {
		String sql = "INSERT INTO student(studentPwd,studentPhone,studentDate)"
				+ "VALUES(?,?,?)";
		return super.update(sql, student.getStudentPwd(),
				student.getStudentPhone(), student.getStudentDate());
	}

	@Override
	public int changePwd(Student student) {
		String sql="update student set studentPwd=? where studentId=?";
		return super.update(sql, student.getStudentPwd(),student.getStudentId());
	}

	@Override
	public String getStudentPhoneById(Integer id) {
		String sql="SELECT studentPhone from student WHERE studentId=?";
		Student student=super.querySingle(sql, new BeanHandler<Student>(Student.class), id);
		return student.getStudentPhone();
	}

}
